package cn.cloudplug.instar_ec.main.index;

import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;

import butterknife.BindView;
import butterknife.OnClick;
import cn.cloudplug.core.util.MyUtil;
import cn.cloudplug.instar_ec.R;
import cn.cloudplug.instar_ec.R2;
import cn.cloudplug.instar_ec.main.base.StatusBarDelegate;
import cn.cloudplug.instar_ec.main.show.SettingDelegate;
import cn.cloudplug.ui.view.OptionItem;
import cn.cloudplug.ui.view.TurnStyleMenu;

/**
 * Created by pc on 2018/1/19.
 */

public class IndexDelegate extends StatusBarDelegate implements ViewTreeObserver.OnGlobalLayoutListener{

    @BindView(R2.id.iv_head)
    View mainActionView;

    @OnClick(R2.id.iv_head)
    void startSettingDelegate(){
        getSupportDelegate().start(new SettingDelegate());
    }

    @BindView(R2.id.rl_item_group)
    cn.cloudplug.ui.view.TurnStyleMenu turnStyleMenu;

    private boolean systemOverlay;
    private boolean isFirst=true;

    public IndexDelegate(){}

    @Override
    protected Object setLayout() {
        return R.layout.delegate_index;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onBindView(Bundle savedInstanceState, View view) {
        initData();
        initView(view);
    }

    private void initView(View view) {
        ViewTreeObserver vto = view.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(this);
    }

    public void initView(){
        turnStyleMenu.centre(getActionViewCenter());
        turnStyleMenu.radius(MyUtil.DipToPixels(getContext(),147));
        turnStyleMenu.invalidateItem();
    }

    @Override
    public void onGlobalLayout() {
        if(isFirst){initView();isFirst=false;}
    }

    private Point getActionViewCoordinates() {
        int[] coords = new int[2];
        // This method returns a x and y values that can be larger than the dimensions of the device screen.
        mainActionView.getLocationOnScreen(coords);

        // So, we need to deduce the offsets.
        if(systemOverlay) {
            coords[1] -= getStatusBarHeight();
        }
        else {
            Rect activityFrame = new Rect();
            getActivityContentView().getWindowVisibleDisplayFrame(activityFrame);
            coords[0] -= (getScreenSize().x - getActivityContentView().getMeasuredWidth());
            coords[1] -= (activityFrame.height() + activityFrame.top - getActivityContentView().getMeasuredHeight());
        }
        return new Point(coords[0], coords[1]);
    }

    public Point getActionViewCenter() {
        Point point = getActionViewCoordinates();
        point.x += mainActionView.getMeasuredWidth() / 2;
        point.y += mainActionView.getMeasuredHeight() / 2;
        return point;
    }

    private void initData() {
        systemOverlay=false;
        turnStyleMenu.addItem(new OptionItem(0,R.mipmap.icon_coordination,"协同","Coordination"));
        turnStyleMenu.addItem(new OptionItem(1,R.mipmap.icon_message,"通讯","Message"));
        turnStyleMenu.addItem(new OptionItem(2,R.mipmap.icon_record,"录音","Record"));
        turnStyleMenu.addItem(new OptionItem(3,R.mipmap.icon_album,"相册","Album"));
        turnStyleMenu.addItem(new OptionItem(4,R.mipmap.icon_tool,"工具","Tool"));
        turnStyleMenu.setOnMenuItemClickListener(new TurnStyleMenu.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(OptionItem item) {
                if(item.getId()==3){
                }
            }
        });
    }

    private Point getScreenSize() {
        Point size = new Point();
        getWindowManager().getDefaultDisplay().getSize(size);
        return size;
    }

    public WindowManager getWindowManager() {
        return (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    }

    public View getActivityContentView() {
        try {
            return getSupportDelegate().getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
        }
        catch(ClassCastException e) {
            throw new ClassCastException("Please provide an Activity context for this FloatingActionMenu.");
        }
    }

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getContext().getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }
}
